package com.liruo.test.spring.annotationdep.bean.importbean;

import com.liruo.test.spring.component.common.importcpt.CommonComponentByImportBeanDefinitionRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Author:liruo
 * @Date:2022-11-20-20:45:01
 * @Desc
 */
@Slf4j
public class ThirdPartyImportBeanDefinitionRegistry implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata ---和ImportSelector一样, 是currentSourceClass.getMetadata()
     *        introspectedClass----导入ThirdPartyImportSelector的注解所在类
     * @return
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        log.info("{}", importingClassMetadata.getClassName());
        boolean beanByImportBeanDefinitionRegistrar = registry.containsBeanDefinition("commonComponentByImportBeanDefinitionRegistry");
        if(!beanByImportBeanDefinitionRegistrar){
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(CommonComponentByImportBeanDefinitionRegistry.class);
            registry.registerBeanDefinition("commonComponentByImportBeanDefinitionRegistry",rootBeanDefinition);
        }
        boolean thirdPartyImportBeanDefinitionRegistry = registry.containsBeanDefinition("thirdPartyImportBeanDefinitionRegistry");
        if(!thirdPartyImportBeanDefinitionRegistry){
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(ThirdPartyImportBeanDefinitionRegistry.class);
            registry.registerBeanDefinition("thirdPartyImportBeanDefinitionRegistry",rootBeanDefinition);
        }
    }

}
